package com.hailong.zyt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hailong.zyt.beans.Student;
import com.hailong.zyt.mapper.*;
import com.hailong.zyt.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hailong
 * @since 2021-05-17
 */
@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    protected StudentMapper studentMapper;
    protected CourseMapper courseMapper;
    protected CourseAllMapper courseAllMapper;
    protected GroupAllMapper groupAllMapper;
    protected GroupMapper groupMapper;



    @Override
    public Integer getCourseCount(Long stuId) {
        Integer count = courseAllMapper.selectCourseCount(stuId);
        return count == null ? 0 : count;
    }

    @Override
    public Integer getGroupCount(Long stuId) {
        Integer count = groupAllMapper.selectGroupCount(stuId);
        return count == null ? 0 : count;
    }

    @Override
    public String getHWFinishedRate(Long stuId) {
/*        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("stu_id", stuId);
        List<Homework> list = homeworkMapper.selectList(queryWrapper);
        int finish = 0;
        for(Homework homework : list) {
            if(homework.getHwState() != 0) finish++;
        }
        String res = "" + finish + " / " + list.size();*/
        return null;
    }


    @Override
    public boolean addStudent(Student newStudent, Model model) {
        try {
            this.save(newStudent);
            log.info("添加状态:{}", "success");
            model.addAttribute("add_msg", "添加成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("添加状态:{}", "fail");
            model.addAttribute("add_msg", "添加失败");
        }
        return false;
    }

    @Override
    public boolean changePassword(Student student, String newPassword, String reNewPassword, Model model) {
        if(!newPassword.equals(reNewPassword)) {
            model.addAttribute("msg_2", "两次密码不一致");
            return false;
        }
        Student oldStudent = this.getById(student.getStuId());
        if(!oldStudent.getStuPassword().equals(student.getStuPassword())) {
            model.addAttribute("msg_1", "原密码不正确");
            return false;
        }
        oldStudent.setStuPassword(newPassword);
        this.updateById(oldStudent);
        return true;
    }

    /*
    * getter/setter
    * */
    @Autowired
    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    public StudentMapper getStudentMapper() {
        return studentMapper;
    }

    public CourseMapper getCourseMapper() {
        return courseMapper;
    }
    @Autowired
    public void setCourseMapper(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    public CourseAllMapper getCourseAllMapper() {
        return courseAllMapper;
    }
    @Autowired
    public void setCourseAllMapper(CourseAllMapper courseAllMapper) {
        this.courseAllMapper = courseAllMapper;
    }

    public GroupAllMapper getGroupAllMapper() {
        return groupAllMapper;
    }
    @Autowired
    public void setGroupAllMapper(GroupAllMapper groupAllMapper) {
        this.groupAllMapper = groupAllMapper;
    }

    public GroupMapper getGroupMapper() {
        return groupMapper;
    }
    @Autowired
    public void setGroupMapper(GroupMapper groupMapper) {
        this.groupMapper = groupMapper;
    }

}
